﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Linq.Expressions;

public partial class Manage_ListInvites : System.Web.UI.Page
{
    protected PagedDataSource pds = new PagedDataSource();


    class A
    {
        public string ID
        {
            get;
            set;
        }
        public string Title
        {
            get;
            set;
        }
        public DateTime Time
        {
            get;
            set;
        }
        public string Status
        {
            get;
            set;
        }
        public bool CanCheck
        {
            get;
            set;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        DateTime now = DateTime.Now;

        CmsDataContext db = new CmsDataContext();
        var q = from aI in db.Invite
                where aI.Belong == Toolkit.CurrentUserBelong
                join aT in db.Train on aI.ID equals aT.InviteID into t
                from aaa in t.DefaultIfEmpty()
                select new A
                {
                    ID = aI.ID,
                    Title = aI.Title,
                    Time = aI.Invited,
                    Status = string.Format("{0}（{1}）", aI.Teacher.Name, (aI.Accepted != null ? "<font color='#ff0000'>应邀</font>" : (DateTime.Now.CompareTo(aI.Invited.AddDays(3)) > 0 ? "<s>已过期</s>" : "<font color='#ff00ff'>未应邀</font>"))),
                    CanCheck = (aaa == null)
                };

        #region 检索条件
        string search = SearchConditionHidden.Value.Trim();
        if (search != "")
        {
            Expression<Func<A, bool>> lmbda = PredicateBuilder<A>.CreatePredicate(new string[] { "Title", "Status"}, search, "aI");
            q = q.Where(lmbda);
        }
        #endregion

        q = q.OrderByDescending(aI => aI.Time);

        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = Toolkit.PageSize;
        pds.DataSource = q.ToList();


        #region 获取当前要显示的页
        int pageIndex = 0;
        pageIndex = Convert.ToInt32(CurrentPage.Value) - 1;
        if (pageIndex >= pds.PageCount)
        {
            pageIndex = pds.PageCount - 1;
        }
        if (pageIndex <= 0)
        {
            pageIndex = 0;
        }
        #endregion

        pds.CurrentPageIndex = pageIndex;

        InvitesView.DataSource = pds;
        InvitesView.DataBind();

        CurrentPage.Value = Convert.ToString(pageIndex + 1);
        SearchCondition.Text = SearchConditionHidden.Value.Trim();
    }
    protected void ChangePage(object sender, CommandEventArgs e)
    {
        string kind = Convert.ToString(e.CommandArgument);
        int p = Convert.ToInt32(CurrentPage.Value);

        if (kind == "F")
        {
            CurrentPage.Value = "1";
        }
        else if (kind == "P")
        {
            p--;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "N")
        {
            p++;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "L")
        {
            CurrentPage.Value = Convert.ToString(Int32.MaxValue);
        }
        else if (kind == "G")
        {
            TextBox gtp;
            foreach (Control c in InvitesView.Controls)
            {
                gtp = c.FindControl("GotoPage") as TextBox;
                if (gtp != null)
                {
                    try
                    {
                        CurrentPage.Value = Convert.ToString(Convert.ToInt32(gtp.Text.Trim()));
                    }
                    catch
                    {
                        //CurrentPage.Value = "1";
                    }

                    break;
                }
            }
        }

        BindData();
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        SearchConditionHidden.Value = SearchCondition.Text.Trim();
        BindData();
    }
    protected void DeleteInvite_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();

        try
        {
            string[] ids = Request.Params["chkID"].Split(',');

            var q = from aI in db.Invite where ids.Contains(aI.ID) select aI;
            foreach (Invite inv in q)
            {
                foreach (Train t in inv.Train)
                {
                    t.InviteID = null;
                }

                db.Invite.DeleteOnSubmit(inv);
            }

            db.SubmitChanges();

            MessageBox.Message = "邀请信息已经删除。";
            MessageBox.Location = "./ListInvites.aspx";
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
        }
    }

    protected void RegisteTrain(object sender, CommandEventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        string iid = Convert.ToString(e.CommandArgument);

        //如果Train中有InviteID的记录，说明已经登记过
        var q = from aT in db.Train
                where aT.InviteID == iid
                select aT;

        if (q.Count() == 0)
        {
            //没有登记过
            Response.Redirect("./EditTrain.aspx?iid=" + iid);
        }
        else
        {
            Response.Redirect("./EditTrain.aspx?tid=" + q.First().ID);
        }
    }
}